home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / lib / C / lsyscache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  11.6 KB  |  613 lines

  1.  
  2. /*    
  3.  *        lsyscache
  4.  *    
  5.  *        Routines to access information within system caches    
  6.  *    $Header: /private/postgres/src/lib/C/RCS/lsyscache.c,v 1.25 1992/07/06 05:03:45 mao Exp $
  7.  */
  8.  
  9.  
  10. /*    
  11.  *            retrieve-cache-attribute
  12.  *            op_class
  13.  *            get_attname
  14.  *            get_attnum
  15.  *            get_atttype
  16.  *            get_opcode
  17.  *            op_mergesortable
  18.  *            op_hashjoinable
  19.  *            get_commutator
  20.  *            get_negator
  21.  *            get_oprrest
  22.  *            get_oprjoin
  23.  *            get_relnatts
  24.  *            get_rel_name
  25.  *            get_typlen
  26.  *            get_typbyval
  27.  *            get_typdefault
  28.  *    
  29.  *     NOTES:
  30.  *        Eventually, the index information should go through here, too.
  31.  *    
  32.  *        Most of these routines call SearchSysCacheStruct() and thus simply
  33.  *        (1) allocate some space for the return struct and (2) call it.
  34.  *    
  35.  */
  36.  
  37. #include "tmp/c.h"
  38.  
  39. #include "nodes/pg_lisp.h"
  40. #include "catalog/syscache.h"
  41. #include "access/att.h"
  42. #include "utils/rel.h"
  43. #include "utils/log.h"
  44. #include "access/attnum.h"
  45.  
  46. #include "catalog/pg_amop.h"
  47. #include "catalog/pg_type.h"
  48. #include "catalog/pg_prs2rule.h"
  49. #include "catalog/pg_prs2stub.h"
  50.  
  51. /* 
  52. require ("cstructs");
  53. require ("cdefs");
  54. require ("cacheids");
  55. */
  56.  
  57. /*    
  58.  *        AttributeGetAttName
  59.  *    
  60.  *        Returns a string.
  61.  *    
  62.  */
  63.  
  64. /*  .. get_attname
  65.  */
  66. Name
  67. AttributeGetAttName (attribute)
  68.      Attribute attribute ;
  69. {
  70. /*
  71.   for (loop = 0;loop + 1(null) = LispNil;(null) = LispNil;) { 
  72.   if ( loop == 16 == val == 0) {
  73.   string (implode (newlist));
  74.   }
  75.   val = attribute_>attname (attribute,loop);
  76.   ;
  77.   if(!val == 0) {
  78.   newlist = append1 (newlist,val);
  79.   }
  80.   }; /*XXX Do List */
  81.   
  82. }
  83.  
  84. /*            ---------- AMOP CACHES ----------
  85.  */
  86.  
  87. /*    
  88.  *        op_class
  89.  *    
  90.  *        Return t iff operator 'opno' is in operator class 'opclass'.
  91.  *    
  92.  */
  93.  
  94. /*  .. in-line-lambda%598040608, match-index-orclause
  95.  */
  96. bool
  97. op_class (opno,opclass)
  98.      ObjectId opno;
  99.      int32 opclass ;
  100. {
  101.     AccessMethodOperatorTupleFormD amoptup;
  102.  
  103.     if (SearchSysCacheStruct (AMOPOPID,&amoptup,opclass,opno,0,0))
  104.       return(true);
  105.     else
  106.       return(false);
  107. }
  108.  
  109. /*            ---------- ATTRIBUTE CACHES ----------
  110.  */
  111.  
  112. /*    
  113.  *        get_attname
  114.  *    
  115.  *        Given the relation id and the attribute number,
  116.  *        return the "attname" field from the attribute relation.
  117.  *    
  118.  */
  119.  
  120. /*  .. fix-parsetree-attnums, parameterize, print_var
  121.  *  .. resno-targetlist-entry
  122.  */
  123.  
  124. Name
  125. get_attname (relid,attnum)
  126.      ObjectId relid;
  127.      AttributeNumber attnum;
  128. {
  129.     AttributeTupleFormD att_tup;
  130.     Name retval = (Name)NULL;
  131.  
  132.     if( SearchSysCacheStruct (ATTNUM,&att_tup,relid,attnum,0,0)) {
  133.     retval = (Name)palloc(sizeof(att_tup.attname));
  134.     bcopy(&(att_tup.attname),retval,sizeof(att_tup.attname));
  135.     return(retval);
  136.     }
  137.     else
  138.       return((Name)NULL);
  139. }
  140.  
  141. /*    
  142.  *        get_attnum
  143.  *    
  144.  *        Given the relation id and the attribute name,
  145.  *        return the "attnum" field from the attribute relation.
  146.  *    
  147.  */
  148.  
  149. /*  .. fix-parsetree-attnums
  150.  */
  151.  
  152. AttributeNumber
  153. get_attnum (relid,attname)
  154.      ObjectId relid;
  155.      Name attname ;
  156. {
  157.     AttributeTupleFormD att_tup;
  158.  
  159.     if(SearchSysCacheStruct (ATTNAME,&att_tup,relid,attname,0,0) ) 
  160.       return(att_tup.attnum);
  161.     else
  162.       return(InvalidAttributeNumber);
  163. }
  164.  
  165. /*    
  166.  *        get_atttype
  167.  *    
  168.  *        Given the relation OID and the attribute number with the relation,
  169.  *        return the attribute type OID.
  170.  *    
  171.  */
  172.  
  173. /*  .. resno-targetlist-entry
  174.  */
  175.  
  176. ObjectId
  177. get_atttype (relid,attnum)
  178.      ObjectId relid;
  179.      AttributeNumber attnum ;
  180. {
  181.     Attribute att_tup = (Attribute)palloc(sizeof(*att_tup));
  182.  
  183.     if( SearchSysCacheStruct (ATTNUM,att_tup,relid,attnum,0,0) ) 
  184.       return(att_tup->atttypid);
  185.     else
  186.       return((ObjectId)NULL);
  187. }
  188.  
  189. /*            ---------- INDEX CACHE ----------
  190.  */
  191.  
  192. /*        watch this space...
  193.  */
  194.  
  195. /*            ---------- OPERATOR CACHE ----------
  196.  */
  197.  
  198. /*    
  199.  *        get_opcode
  200.  *    
  201.  *        Returns the regproc id of the routine used to implement an
  202.  *        operator given the operator uid.
  203.  *    
  204.  */
  205.  
  206. /*  .. create_hashjoin_node, create_mergejoin_node, relation-sortkeys
  207.  *  .. replace_opid, set-temp-tlist-operators
  208.  */
  209.  
  210.  
  211. RegProcedure
  212. get_opcode (opno)
  213.      ObjectId opno;
  214. {
  215.     OperatorTupleFormD optup;
  216.  
  217.     if( SearchSysCacheStruct (OPROID,&optup,opno,0,0,0) ) 
  218.       return(optup.oprcode);
  219.     else
  220.       return((RegProcedure)NULL);
  221. }
  222.  
  223. NameData
  224. get_opname(opno)
  225. ObjectId opno;
  226. {
  227.     OperatorTupleFormD optup;
  228.  
  229.     if (SearchSysCacheStruct(OPROID,&optup,opno,0,0,0))
  230.        return(optup.oprname);
  231.     else
  232.        elog(WARN, "can't look up operator %d\n", opno);
  233. }
  234.  
  235. /*    
  236.  *        op_mergesortable
  237.  *    
  238.  *        Returns the left and right sort operators and types corresponding to a
  239.  *        mergesortable operator, or nil if the operator is not mergesortable.
  240.  *    
  241.  */
  242.  
  243. /*  .. mergesortop
  244.  */
  245.  
  246. LispValue
  247. op_mergesortable (opno,ltype,rtype)
  248.      ObjectId opno;
  249.      ObjectId ltype,rtype ;
  250. {
  251.     OperatorTupleFormD optup;
  252.  
  253.     if(SearchSysCacheStruct (OPROID,&optup,opno,0,0,0) &&
  254.        optup.oprlsortop &&
  255.        optup.oprrsortop && 
  256.        optup.oprleft == ltype &&
  257.        optup.oprright == rtype) 
  258.       return (lispCons ((LispValue)(optup.oprlsortop),
  259.                    lispCons ((LispValue)(optup.oprrsortop),LispNil)));
  260.     else
  261.       return(LispNil);
  262. }
  263.  
  264. /*    
  265.  *        op_hashjoinable
  266.  *    
  267.  *        Returns the hash operator corresponding to a hashjoinable operator, 
  268.  *        or nil if the operator is not hashjoinable.
  269.  *    
  270.  */
  271.  
  272. /*  .. hashjoinop
  273.  */
  274. ObjectId
  275. op_hashjoinable (opno,ltype,rtype)
  276.      ObjectId opno,ltype,rtype ;
  277. {
  278.     OperatorTupleFormD optup;
  279.  
  280.     if (SearchSysCacheStruct (OPROID,&optup,opno,0,0,0) && 
  281.     optup.oprcanhash  &&
  282.     optup.oprleft == ltype &&
  283.     optup.oprright == rtype) 
  284.       return(opno);
  285.     else
  286.       return((ObjectId)NULL);
  287. }
  288.  
  289. /*    
  290.  *        get_commutator
  291.  *    
  292.  *        Returns the corresponding commutator of an operator.
  293.  *    
  294.  */
  295.  
  296. /*  .. in-line-lambda%598040608
  297.  */
  298. ObjectId
  299. get_commutator (opno)
  300.      ObjectId opno ;
  301. {
  302.     OperatorTupleFormD optup;
  303.  
  304.     if(SearchSysCacheStruct (OPROID,&optup,opno,0,0,0))
  305.       return(optup.oprcom);
  306.     else
  307.       return((ObjectId)NULL);
  308. }
  309.  
  310. /*  .. CommuteClause()
  311.  */
  312. HeapTuple
  313. get_operator_tuple (opno)
  314.      ObjectId opno ;
  315. {
  316.     HeapTuple optup;
  317.  
  318.     if((optup = SearchSysCacheTuple (OPROID,opno,0,0,0)))
  319.       return(optup);
  320.     else
  321.       return((HeapTuple)NULL);
  322. }
  323.  
  324. /*    
  325.  *        get_negator
  326.  *    
  327.  *        Returns the corresponding negator of an operator.
  328.  *    
  329.  */
  330.  
  331. /*  .. push-nots
  332.  */
  333.  
  334. ObjectId
  335. get_negator (opno)
  336.      ObjectId opno ;
  337. {
  338.     OperatorTupleFormD optup;
  339.  
  340.     if(SearchSysCacheStruct (OPROID,&optup,opno,0,0,0))
  341.       return(optup.oprnegate);
  342.     else
  343.       return((ObjectId)NULL);
  344. }
  345.  
  346. /*    
  347.  *        get_oprrest
  348.  *    
  349.  *        Returns procedure id for computing selectivity of an operator.
  350.  *    
  351.  */
  352.  
  353. /*  .. compute_selec
  354.  */
  355. RegProcedure
  356. get_oprrest (opno)
  357.      ObjectId opno ;
  358. {
  359.     OperatorTupleFormD optup;
  360.  
  361.     if(SearchSysCacheStruct (OPROID,&optup,opno,0,0,0))
  362.       return(optup.oprrest );
  363.     else
  364.       return((RegProcedure) NULL);
  365. }
  366.  
  367. /*    
  368.  *        get_oprjoin
  369.  *    
  370.  *        Returns procedure id for computing selectivity of a join.
  371.  *    
  372.  */
  373.  
  374. /*  .. compute_selec
  375.  */
  376.  
  377. RegProcedure
  378. get_oprjoin (opno)
  379.      ObjectId opno ;
  380. {
  381.     OperatorTupleFormD optup;
  382.  
  383.     if(SearchSysCacheStruct (OPROID,&optup,opno,0,0,0))
  384.       return(optup.oprjoin);
  385.     else
  386.       return((RegProcedure)NULL);
  387. }
  388.  
  389. /*            ---------- RELATION CACHE ----------
  390.  */
  391.  
  392. /*    
  393.  *        get_relnatts
  394.  *    
  395.  *        Returns the number of attributes for a given relation.
  396.  *    
  397.  */
  398.  
  399. /*  .. expand-targetlist, write-decorate
  400.  */
  401.  
  402. AttributeNumber
  403. get_relnatts (relid)
  404.      ObjectId relid ;
  405. {
  406.     RelationTupleFormD reltup;
  407.  
  408.     if(SearchSysCacheStruct (RELOID,&reltup,relid,0,0,0))
  409.     return(reltup.relnatts);
  410.     else
  411.         return(InvalidAttributeNumber);
  412. }
  413.  
  414. /*    
  415.  *        get_rel_name
  416.  *    
  417.  *        Returns the name of a given relation.
  418.  *    
  419.  */
  420.  
  421. /*  .. ExecOpenR, new-rangetable-entry
  422.  */
  423.  
  424. Name
  425. get_rel_name (relid)
  426.      ObjectId relid ;
  427. {
  428.     RelationTupleFormD reltup;
  429.     Name retval = (Name)NULL;
  430.  
  431.     if((SearchSysCacheStruct (RELOID,&reltup,relid,0,0,0))) {
  432.     retval = (Name)palloc(sizeof(reltup.relname));
  433.     bcopy(&(reltup.relname),retval,sizeof(reltup.relname));
  434.     return (retval);
  435.     } else
  436.       return((Name)NULL);
  437. }
  438.  
  439. /*
  440.  * get_relstub
  441.  * return the stubs associated with a relation.
  442.  * NOTE: we return a pointer to a *copy* of the stubs.
  443.  * NOTE2: I have added an extra return argument: 'islastP'
  444.  */
  445. struct varlena *
  446. get_relstub (relid, no, islastP)
  447.      ObjectId relid ;
  448.      int no;
  449.      bool *islastP;
  450. {
  451.     HeapTuple tuple;
  452.     Form_pg_prs2stub prs2stubStruct;
  453.     struct varlena * retval;
  454.     struct varlena * relstub;
  455.  
  456.     tuple = SearchSysCacheTuple(PRS2STUB,relid,no,0,0);
  457.  
  458.     if (HeapTupleIsValid(tuple)) {
  459.     prs2stubStruct = (Form_pg_prs2stub) GETSTRUCT(tuple);
  460.     /*
  461.      * NOTE:
  462.      * skip the 4 first bytes to get the actual
  463.      * varlena struct
  464.      *
  465.      * This was only need because we used to store the size twice
  466.      * which I have now fixed -mer 2 April 1992
  467.      *
  468.      * relstub = (struct varlena *) PSIZESKIP( & (prs2stubStruct->prs2stub) );
  469.      */
  470.     relstub = (struct varlena *) &(prs2stubStruct->prs2stub);
  471.     retval = (struct varlena *)palloc(VARSIZE( relstub ));
  472.     bcopy(relstub, retval, VARSIZE( relstub ));
  473.     if (prs2stubStruct->prs2islast)
  474.         *islastP = true;
  475.     else
  476.         *islastP = false;
  477.     return(retval);
  478.     } else {
  479.       return((struct varlena *)NULL);
  480.     }
  481. }
  482.  
  483. /*
  484.  * get_ruleid
  485.  * given a tuple level rule name, return its oid.
  486.  */
  487. ObjectId
  488. get_ruleid(ruleName)
  489. Name ruleName;
  490. {
  491.     HeapTuple tuple;
  492.  
  493.     tuple = SearchSysCacheTuple(PRS2RULEID,ruleName,0,0,0);
  494.     if (HeapTupleIsValid(tuple)) {
  495.     return(tuple->t_oid);
  496.     } else {
  497.     return(InvalidObjectId);
  498.     }
  499. }
  500.  
  501. /*
  502.  * get_eventrelid
  503.  * given a (tuple system) rule oid, return the oid of the relation
  504.  * where this rule is defined on.
  505.  */
  506. ObjectId
  507. get_eventrelid(ruleId)
  508. ObjectId ruleId;
  509. {
  510.     HeapTuple tuple;
  511.     Form_pg_prs2rule prs2ruleStruct;
  512.  
  513.     tuple = SearchSysCacheTuple(PRS2EVENTREL,ruleId,0,0,0);
  514.     if (HeapTupleIsValid(tuple)) {
  515.     prs2ruleStruct = (Form_pg_prs2rule) GETSTRUCT(tuple);
  516.     return(prs2ruleStruct->prs2eventrel);
  517.     } else {
  518.     return(InvalidObjectId);
  519.     }
  520. }
  521.  
  522.  
  523. /*            ---------- TYPE CACHE ----------
  524.  */
  525.  
  526. /*    
  527.  *        get_typlen
  528.  *    
  529.  *        Given the type OID, return the length of the type.
  530.  *    
  531.  */
  532.  
  533. /*  .. compute-attribute-width, create_tl_element, flatten-tlist
  534.  *  .. resno-targetlist-entry, substitute-parameters
  535.  */
  536.  
  537. int16
  538. get_typlen (typid)
  539.      ObjectId typid;
  540. {
  541.     TypeTupleFormD typtup;
  542.  
  543.     if (SearchSysCacheStruct (TYPOID,&typtup,typid,0,0,0))
  544.       return(typtup.typlen);
  545.     else
  546.       return((int16)NULL);
  547. }
  548.  
  549. /*    
  550.  *        get_typbyval
  551.  *    
  552.  *        Given the type OID, determine whether the type is returned by value or
  553.  *        not.  Returns 1 if by value, 0 if by reference.
  554.  *    
  555.  */
  556.  
  557. /*  .. ExecTypeFromTL
  558.  */
  559.  
  560. bool
  561. get_typbyval (typid)
  562.      ObjectId typid ;
  563. {
  564.     TypeTupleFormD typtup;
  565.  
  566.     if(SearchSysCacheStruct (TYPOID,&typtup,typid,0,0,0))
  567.       return((bool)typtup.typbyval);
  568.     else
  569.       return(false);
  570. }
  571.  
  572. /*    
  573.  *        get_typdefault
  574.  *    
  575.  *        Given the type OID, return the default value of the ADT.
  576.  *    
  577.  */
  578.  
  579. /*  .. resno-targetlist-entry
  580.  */
  581.  
  582. struct varlena *
  583. get_typdefault (typid)
  584.      ObjectId typid ;
  585. {
  586.     struct varlena *typdefault = 
  587.       (struct varlena *)TypeDefaultRetrieve (typid);
  588.     return(typdefault);
  589. }
  590.  
  591. /*    
  592.  *        get_typtype
  593.  *    
  594.  *        Given the type OID, find if it is a basic type, a named relation
  595.  *    or the generic type 'relation'.
  596.  *    It returns the null char if the cache lookup fails...
  597.  *    
  598.  */
  599.  
  600. char
  601. get_typtype (typid)
  602.      ObjectId typid ;
  603. {
  604.     TypeTupleFormD typtup;
  605.  
  606.     if(SearchSysCacheStruct (TYPOID,&typtup,typid,0,0,0)) {
  607.       return(typtup.typtype);
  608.     } else {
  609.       return('\0');
  610.     }
  611. }
  612.  
  613.